package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberTeamBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.MemberFindPackersEntity;
import com.sgb.member.ui.activity.MemberBusinessActivity;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.view_model.BusinessTeamModel;
import com.sgb.member.weight.threefliter.MThreeLevelFilterWindow;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.CopyUtils;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.view.selection.XChooseDialog;
import com.swgk.core.view.selection.XChooseEntity;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class BusinessTeamFragment extends AppLazyFragment implements MemberStatusView.OnChooseListener, XChooseDialog.DialogCallBackImpl {

    @Inject
    public BusinessTeamModel sModel;
    private FragmentMemberTeamBinding mBinding;
    private HomeOtherMemberAdapter<MemberFindPackersEntity> mAdapter;
    private int mCurPos; // 主页面TAB下标
    private CommonPopEntity mCurEntity; // 主页面TAB下标
    // 经营地址Popup
    private ThreeLevelAreaWindow mAddressPop;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;

    private String mNo1 = ""; // 1, "工程分类");
    private String mNo2 = ""; // 2, "工程专项");
    private String mNo3 = ""; // 3, "专业分类");
    private String mNo4 = ""; // 4, "团队规模");
    private String mNo6 = ""; // 6, "所在地区");
    private String mNo7 = ""; // 7, "所在地");
    private String mNo9 = "";//会员级别
    /**
     * 工程分类
     */
    private MThreeLevelFilterWindow popCategory;


    public static BusinessTeamFragment newInstance(int position, CommonPopEntity entity) {
        BusinessTeamFragment fragment = new BusinessTeamFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("ENTITY", entity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_team, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        mCurEntity = (CommonPopEntity) getArguments().getSerializable("ENTITY");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.chooseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < mNameArray.size(); i++) {
                    XChooseEntity chooseEntity = mData.get(mData.keyAt(i));
                    if (chooseEntity != null) {
                        if (chooseEntity.getList() == null || chooseEntity.getList().size() == 0) {
                            if (chooseEntity.getSort() == PickCateType.TYPE_1 ||
                                    chooseEntity.getSort() == PickCateType.TYPE_4 ||
                                    chooseEntity.getSort() == PickCateType.TYPE_6) {
                                loadOtherData();
                                return;
                            }
                        }
                    }
                }
                mChooseDialog.initData(mData, mFilters);
                mChooseDialog.showDialog();
            }
        });
        initChooseDialog();
        initPopup();
        initRecycler();
    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
        loadOtherData();
    }

    public void loadData(boolean isLoadMore) {
        String text = ((MemberBusinessActivity) getActivity()).getSearchText();
        sModel.teamList(isLoadMore, text, mCurEntity.getNo(), mNo1, mNo2, mNo3, mNo4, mNo6, mNo7,mNo9);
    }

    private void loadOtherData() {
        if (isListEmpty(PickCateType.TYPE_1)) {
            sModel.getCategoryTrees(PickCateType.TYPE_1, false, false);
        }
        if (isListEmpty(PickCateType.TYPE_4)) {
            sModel.getTeamScale(PickCateType.TYPE_4, mCurPos == 0, false, false);
        }
        if (scrData == null || scrData.size() == 0) {
            sModel.getAddressModel(PickCateType.TYPE_6, false);  // 经营地址
        }
        if (isListEmpty(PickCateType.TYPE_9)) {
            sModel.getMemberLevelData(PickCateType.TYPE_9, false); // 会员级别
        }
    }

    public void setAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            mAddressPop.setData(scrData, true);
        }
    }

    /**
     * -------------------------------------initRecycler------------------------------------------
     */
    private void initRecycler() {
        TextView textView = mBinding.emptyView.findViewById(R.id.no_data_count);
        textView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        initRecyclerAdapter(0);
    }

    private void initRecyclerAdapter(int index) {
        DividerItemDecoration divider3 = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider3.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide2));
        mBinding.recyclerView.addItemDecoration(divider3);
        mAdapter = new HomeOtherMemberAdapter<MemberFindPackersEntity>(getActivity(), R.layout.item_other_team, null);
        mAdapter.setOnItemClick(new AdapterOnItemClick<MemberFindPackersEntity>() {
            @Override
            public void onItemClick(MemberFindPackersEntity findPackersEntity, int position) {
                sModel.getH5Url(findPackersEntity.getNo());
            }
        });
        if (mAdapter != null) {
            mBinding.recyclerView.setAdapter(mAdapter);
        }
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        mAdapter.clearDatas();
        mBinding.emptyView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<MemberFindPackersEntity> list, boolean isLoadMore, boolean noMoreData) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setNoMore(noMoreData);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * ------------------------------- Popup ---------------------------------------------
     */
    private void initPopup() {
        mBinding.chooseLayout.setVisibility(View.GONE);
        mBinding.popup1.setVisibility(View.VISIBLE);
        mBinding.popup1.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup2.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup3.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup21.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popupMemberLever.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        mBinding.popup1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isListEmpty(PickCateType.TYPE_1)) {
                    sModel.getCategoryTrees(PickCateType.TYPE_1, false, true);
                } else {
                    showCategoryPop();
                }
            }
        });
        mBinding.popup3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scrData == null) {
                    sModel.getAddressModel(PickCateType.TYPE_6, true);
                } else {
                    showAddressPopup();
                }
            }
        });
        initAddressPopup();
        initCateGoryPopup();
    }

    private void initAddressPopup() {
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(mBinding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                mNo6 = "";
                mNo7 = "";
                mFilters.put(PickCateType.TYPE_6, mAddressPop.getProvincePosition());
                addMainEntity(PickCateType.TYPE_7, new ArrayList<>());
                mBinding.popup3.updateChooseTextStatus(0, mBinding.popup3.getKeyName());
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    mNo6 = entity.getProvinceNo();
                    mFilters.put(PickCateType.TYPE_6, mAddressPop.getProvincePosition());
                    mBinding.popup3.updateChooseTextStatus(1, entity.getProvinceName());
                    updateCityData(new CommonPopEntity(entity.getProvinceNo(), entity.getProvinceName()), mAddressPop.getProvincePosition(), false);
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        mNo7 = entity.getCityNo();
                        mFilters.put(PickCateType.TYPE_7, mAddressPop.getmCityPosition());
                        mBinding.popup3.updateChooseTextStatus(1, entity.getCityName());
                    }
                }
                //刷新数据
                loadData(false);
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            mBinding.popLine.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(mNo6) || !TextUtils.isEmpty(mNo7)) {
                mBinding.popup3.updateChooseTextStatus(1, null);
            } else {
                mBinding.popup3.updateChooseTextStatus(0, mBinding.popup3.getKeyName());
            }
        });
    }

    private void initCateGoryPopup() {
        popCategory = new MThreeLevelFilterWindow(getActivity());
        popCategory.setBgView(mBinding.grayLayout);
        popCategory.setOnResultClick(new MThreeLevelFilterWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                CommonPopEntity pickResultProject1 = (CommonPopEntity) key1;
                CommonPopEntity pickResultProject2 = (CommonPopEntity) key2;
                CommonPopEntity pickResultProject3 = (CommonPopEntity) key3;
                mNo1 = pickResultProject1.getNo();
                mNo2 = pickResultProject2.getNo();
                mNo3 = pickResultProject3.getNo();
                mFilters.put(PickCateType.TYPE_1, pickResultProject1.getCurPos());
                updateMultiCategoryData(PickCateType.TYPE_1, pickResultProject1, pickResultProject1.getCurPos(), false);
                updateMultiCategoryData(PickCateType.TYPE_2, pickResultProject2, pickResultProject2.getCurPos(), false);
                mBinding.popup1.updateChooseTextStatus(0, mBinding.popup1.getKeyName());
                if (!TextUtils.isEmpty(mNo1)) {
                    mBinding.popup1.updateChooseTextStatus(1, pickResultProject1.getTypeName());
                    mFilters.put(PickCateType.TYPE_1, pickResultProject1.getCurPos());
                    if (!TextUtils.isEmpty(mNo2)) {
                        mBinding.popup1.updateChooseTextStatus(1, pickResultProject2.getTypeName());
                        mFilters.put(PickCateType.TYPE_2, pickResultProject2.getCurPos());
                        if (!TextUtils.isEmpty(mNo3)) {
                            mBinding.popup1.updateChooseTextStatus(1, pickResultProject3.getTypeName());
                            mFilters.put(PickCateType.TYPE_3, pickResultProject3.getCurPos());
                        }
                    }
                }
                loadData(false);
            }

            @Override
            public void reset() {
                mNo1 = "";
                mNo2 = "";
                mNo3 = "";
                mFilters.put(PickCateType.TYPE_1, 0);
                addMainEntity(PickCateType.TYPE_2, new ArrayList<>());
                addMainEntity(PickCateType.TYPE_3, new ArrayList<>());
                mBinding.popup1.updateChooseTextStatus(0, mBinding.popup1.getKeyName());
                loadData(false);
            }
        });
        popCategory.setOnDismissListener(() -> {
            if (!TextUtils.isEmpty(mNo1) || !TextUtils.isEmpty(mNo2) || !TextUtils.isEmpty(mNo3)) {
                mBinding.popup1.updateChooseTextStatus(1, null);
            } else {
                mBinding.popup1.updateChooseTextStatus(0, mBinding.popup1.getKeyName());
            }
        });
    }

    private void showAddressPopup() {
        mBinding.popLine.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mNo6) || !TextUtils.isEmpty(mNo7)) {
            mBinding.popup3.updateChooseTextStatus(-2, null);
        } else {
            mBinding.popup3.updateChooseTextStatus(-1, mBinding.popup3.getKeyName());
        }
        mAddressPop.showPopwindow0(mBinding.popLine);
    }

    /**
     * 工程-工程分类
     */
    public void showCategoryPop() {
        mBinding.popLine.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mNo1)) {
            mBinding.popup1.updateChooseTextStatus(-2, null);
        } else {
            mBinding.popup1.updateChooseTextStatus(-1, mBinding.popup1.getKeyName());
        }
        popCategory.showPopwindow0(mBinding.popLine);
    }

    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "工程分类":
                /* sModel.getCategoryTrees(PickCateType.TYPE_1, false);*/
                break;
            case "团队规模":
                sModel.getTeamScale(PickCateType.TYPE_4, mCurPos == 0, false, true);
                break;
        }
    }

    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        switch (mKeyName) {
            case "工程分类":
                /*if (!TextUtils.equals(no, mNo1)) {
                    mNo1 = no;
                    mFilters.put(PickCateType.TYPE_1, position);
                    updateMultiCategoryData(PickCateType.TYPE_1, entity, position, false);
                    result(mData, mFilters);
                }*/
                break;
            case "团队规模":
                if (!TextUtils.equals(no, mNo4)) {
                    mNo4 = no;
                    mFilters.put(PickCateType.TYPE_4, position);
                    loadData(false);
                }
                break;
            case "会员级别":
                if (!TextUtils.equals(no, mNo9)) {
                    mNo9 = entity.getId();
                    mFilters.put(PickCateType.TYPE_9, position);
                    loadData(false);
                }
                break;
        }
    }

    /**
     * ------------------------------------ 侧滑刷选 ----------------------------------------
     */

    private XChooseDialog mChooseDialog;
    // 数据集
    private SparseArray<XChooseEntity> mData = new SparseArray<>();
    // 本地缓存筛选结果
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> mNameArray;

    private void initChooseDialog() {
        mNameArray = new SparseArray<>();
        mNameArray.put(PickCateType.TYPE_1, "工程分类");
        mNameArray.put(PickCateType.TYPE_2, "工程专项");
        mNameArray.put(PickCateType.TYPE_3, "专业分类");
        mNameArray.put(PickCateType.TYPE_4, "团队规模");
        mNameArray.put(PickCateType.TYPE_6, "所在地区");
        mNameArray.put(PickCateType.TYPE_7, "所在地");
        mNameArray.put(PickCateType.TYPE_9, "会员级别");
        mChooseDialog = new XChooseDialog(getContext(), mNameArray);
        mChooseDialog.setCallBack(this);
        for (int i = 0; i < mNameArray.size(); i++) {
            int key = mNameArray.keyAt(i);
            addMainEntity(key, new ArrayList<>());
        }
    }

    public void setChooseItemData(int type, List<CommonPopEntity> data, boolean isShowPopup) {
        addMainEntity(type, data);
        switch (type) {
            case PickCateType.TYPE_1:
                popCategory.setData(data, 0);
                if (isShowPopup) {
                    showCategoryPop();
                }
                break;
            case PickCateType.TYPE_2:
                break;
            case PickCateType.TYPE_4:
                mBinding.popup2.setData(data, mNo4);
                break;
            case PickCateType.TYPE_6:
                if (isShowPopup) {
                    showAddressPopup();
                }
                break;
            case PickCateType.TYPE_9:
                mBinding.popupMemberLever.setData(data, mNo9);
                break;
        }
    }

    public void showLeftDialog(int type, List<CommonPopEntity> data) {
        mChooseDialog.addEntity(type, data);
        mChooseDialog.showDialog();
    }

    public void addMainEntity(int index, List<CommonPopEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setList(new ArrayList<>());
            } else {
                mData.get(index).setList(list);
            }
        } else {
            mData.put(index, new XChooseEntity(index, mNameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    private boolean isListEmpty(int catType) {
        return mData.get(catType) != null &&
                (mData.get(catType).getList() == null || mData.get(catType).getList().size() == 0);
    }

    @Override
    public void loadChildData(XChooseEntity entity, int pos) {
        switch (entity.getSort()) {
            case PickCateType.TYPE_1:
            case PickCateType.TYPE_2:
            case PickCateType.TYPE_3:
                updateMultiCategoryData(entity.getSort(), entity.getList().get(pos), pos, true);
                mChooseDialog.showDialog();
                break;
            case PickCateType.TYPE_6:
                updateCityData(entity.getList().get(pos), pos, true);
                mChooseDialog.showDialog();
                break;
            default:
                break;
        }
    }

    private void updateMultiCategoryData(int sort, CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        ArrayList<CommonPopEntity> objects = new ArrayList<>();
        if (isShowLeftDialog) {
            switch (sort) {
                case PickCateType.TYPE_1:
                    mChooseDialog.addEntity(PickCateType.TYPE_2, objects);
                    mChooseDialog.addEntity(PickCateType.TYPE_3, objects);
                    break;
                case PickCateType.TYPE_2:
                    mChooseDialog.addEntity(PickCateType.TYPE_3, objects);
                    break;
                case PickCateType.TYPE_3:
                    break;
            }
            if (pos > 0) {
                switch (sort) {
                    case PickCateType.TYPE_1:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                                entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                            }
                            mChooseDialog.addEntity(PickCateType.TYPE_2, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_2, objects);
                        }
                        break;
                    case PickCateType.TYPE_2:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                                entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                            }
                            mChooseDialog.addEntity(PickCateType.TYPE_3, entity.getChildren());
                        } else {
                            mChooseDialog.addEntity(PickCateType.TYPE_3, objects);
                        }
                        break;
                    case PickCateType.TYPE_3:
                        break;
                }
            }
        } else {
            switch (sort) {
                case PickCateType.TYPE_1:
                    addMainEntity(PickCateType.TYPE_2, objects);
                    addMainEntity(PickCateType.TYPE_3, objects);
                    break;
                case PickCateType.TYPE_2:
                    addMainEntity(PickCateType.TYPE_3, objects);
                    break;
                case PickCateType.TYPE_3:
                    break;
            }
            if (pos > 0) {
                switch (sort) {
                    case PickCateType.TYPE_1:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                                entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                            }
                            addMainEntity(PickCateType.TYPE_2, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_2, objects);
                        }
                        break;
                    case PickCateType.TYPE_2:
                        if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                            if (!entity.getChildren().get(0).getTypeName().equals("全部") && !entity.getChildren().get(0).getTypeName().equals("不限")) {
                                entity.getChildren().add(0, new CommonPopEntity("", "全部"));
                            }
                            addMainEntity(PickCateType.TYPE_3, entity.getChildren());
                        } else {
                            addMainEntity(PickCateType.TYPE_3, objects);
                        }
                        break;
                    case PickCateType.TYPE_3:
                        break;
                }
            }
        }
    }

    private void updateCityData(CommonPopEntity entity, int pos, boolean isShowLeftDialog) {
        if (isShowLeftDialog) {
            mChooseDialog.addEntity(PickCateType.TYPE_7, new ArrayList<>());
        } else {
            addMainEntity(PickCateType.TYPE_7, new ArrayList<>());
        }
        if (pos > 0) {
            sModel.getAddressCityData(scrData, entity.getNo(), PickCateType.TYPE_7, isShowLeftDialog);
        }
    }

    @Override
    public void result(SparseArray<XChooseEntity> data, SparseIntArray map) {
        mData = CopyUtils.deepPopEntityCopy(data);
        mFilters = map.clone();
        for (int i = 0; i < mData.size(); i++) {
            XChooseEntity entity = mData.get(mData.keyAt(i));
            int key = entity.getSort();
            int value = mFilters.get(key);
            switch (key) {
                case PickCateType.TYPE_1:
                    mNo1 = entity.getItemNo(value);
                    mBinding.popup1.refreshChoose(mNo1, entity.getItemName(value), value);
                    popCategory.setDefaultItem(value,0,0);
                    break;
                case PickCateType.TYPE_2:
                    mNo2 = entity.getItemNo(value);
                    if (value > 0) {
                        popCategory.setDefaultItem(mFilters.get(PickCateType.TYPE_1),value,0);
                        mBinding.popup1.refreshChoose(mNo2, entity.getItemName(value), value);
                    }
                    break;
                case PickCateType.TYPE_3:
                    mNo3 = entity.getItemNo(value);
                    if (value > 0) {
                        popCategory.setDefaultItem(mFilters.get(PickCateType.TYPE_1),mFilters.get(PickCateType.TYPE_2),value);
                        mBinding.popup1.refreshChoose(mNo3, entity.getItemName(value), value);
                    }
                    break;
                case PickCateType.TYPE_4:
                    mNo4 = entity.getItemNo(value);
                    mBinding.popup2.refreshChoose(mNo4, entity.getItemName(value), value);
                    break;
                case PickCateType.TYPE_6:
                    mNo6 = entity.getItemNo(value);
                    mBinding.popup3.refreshChoose(mNo6, entity.getItemName(value), value);
                    mAddressPop.setDefPosition(value, 0);
                    break;
                case PickCateType.TYPE_7:
                    mNo7 = entity.getItemNo(value);
                    mAddressPop.setDefPosition(mFilters.get(PickCateType.TYPE_6), value);
                    if (value > 0) {
                        mBinding.popup3.refreshChoose(mNo7, entity.getItemName(value), value);
                    }
                    break;
                case PickCateType.TYPE_9:
                    mNo9 = entity.getItemNo(value);
                    mBinding.popupMemberLever.refreshChoose(mNo9, entity.getItemName(value), value);
                    break;
                default:
                    break;
            }
        }
        loadData(false);
    }

    @Override
    public void reset() {
        mFilters.clear();
        resetMainData();
        result(mData, mFilters);
        mChooseDialog.initData(mData, mFilters);
        mChooseDialog.showDialog();
    }

    private void resetMainData() {
        addMainEntity(PickCateType.TYPE_2, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_3, new ArrayList<>());
        addMainEntity(PickCateType.TYPE_7, new ArrayList<>());
    }

    @Override
    public void onCancel() {
        mChooseDialog.initData(mData, mFilters);
    }
}
